#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> generate_min_form(const vector<int> &faces) {
    vector<vector<int>> all_perms;
    int a = faces[0], b = faces[1], c = faces[2], d = faces[3], e = faces[4], f = faces[5];

    // 生成所有可能的24种排列方式
    all_perms.push_back({a, b, c, d, e, f});
    all_perms.push_back({a, b, d, c, f, e});
    all_perms.push_back({a, b, e, f, d, c});
    all_perms.push_back({a, b, f, e, c, d});

    all_perms.push_back({b, a, c, d, e, f});
    all_perms.push_back({b, a, d, c, f, e});
    all_perms.push_back({b, a, e, f, d, c});
    all_perms.push_back({b, a, f, e, c, d});

    all_perms.push_back({c, d, a, b, e, f});
    all_perms.push_back({c, d, b, a, f, e});
    all_perms.push_back({c, d, e, f, a, b});
    all_perms.push_back({c, d, f, e, b, a});

    all_perms.push_back({d, c, a, b, e, f});
    all_perms.push_back({d, c, b, a, f, e});
    all_perms.push_back({d, c, e, f, a, b});
    all_perms.push_back({d, c, f, e, b, a});

    all_perms.push_back({e, f, a, b, c, d});
    all_perms.push_back({e, f, b, a, d, c});
    all_perms.push_back({e, f, c, d, a, b});
    all_perms.push_back({e, f, d, c, b, a});

    all_perms.push_back({f, e, a, b, c, d});
    all_perms.push_back({f, e, b, a, d, c});
    all_perms.push_back({f, e, c, d, a, b});
    all_perms.push_back({f, e, d, c, b, a});

    // 找到字典序最小的排列
    vector<int> min_perm = all_perms[0];
    for (const auto &perm: all_perms) {
        if ( perm < min_perm ) {
            min_perm = perm;
        }
    }
    return min_perm;
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int N;
        cin >> N;
        vector<vector<int>> dice;
        for (int i = 0; i < N; ++i) {
            vector<int> face(6);
            for (int j = 0; j < 6; ++j) {
                cin >> face[j];
            }
            dice.push_back(generate_min_form(face));
        }

        bool all_same = true;
        vector<int> first = dice[0];
        for (int i = 1; i < N; ++i) {
            if ( dice[i] != first ) {
                all_same = false;
                break;
            }
        }
        cout << (all_same ? "yes" : "no") << endl;
    }
    return 0;
}
